package van.planifolia.work2;

import van.planifolia.ListConstant;
import van.planifolia.People;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by Intellij IDEA<br>
 *
 * @author Planifolia.Van
 * @version 1.0
 * @date 2022/11/6 16:59
 */
public class Collect {
    public static void main(String[] args) {
        // 这里我们会演示使用Collect来进行流的计算以及流的转换成新集合
        // 先是集合的计算,分组
        Map<String, List<People>> collect = ListConstant.peoples.stream().collect(Collectors.groupingBy(People::getAddress));
        collect.forEach((k,v)->{
            System.out.println("地区为:" + k);
            System.out.println("成员为：");
            System.out.println(v);
        });
        // 求年龄最大的人元信息
        System.out.println(ListConstant.peoples.stream().collect(Collectors.maxBy(Comparator.comparingInt(People::getAge))));
        // 求和
        Integer[] integers = {1,2,3,4};
        System.out.println(Arrays.stream(integers).collect(Collectors.summingInt(Integer::intValue)));
        // 求平均值
        System.out.println(Arrays.stream(integers).collect(Collectors.averagingInt(Integer::intValue)));

        //集合转换，转map
        ListConstant.peoples.stream()
                .collect(Collectors.toMap(People::getName,people -> people))
                .forEach((k,v)->{
                    System.out.println("姓名:" + k);
                    System.out.println("信息：");
                    System.out.println(v);
                });
        // 转set
        System.out.println(Arrays.stream(integers).collect(Collectors.toSet()));
        // 转list
        System.out.println(Arrays.stream(integers).collect(Collectors.toList()));
    }
}
